1335C - Two Teams Composing - CodeForces Solution


binary search greedy implementation sortings *1100

Please click on ads to support us..

Python Code:

t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    cnt=[0]*(len(a)+1)
    for x in a:
        cnt[x]+=1
        
    l = len(set(a))
    m = max(cnt)
    if l==m:
        print(m-1)
    else:
        print(min(l,m))

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define fast() ios_base::sync_with_stdio(0); cin.tie(0)
#define vec vector<ll>
#define pb  push_back
#define alls(s) s.begin(),s.end()
#define allv(v) v.begin(),v.end()
#define allv2(v2) v2.begin(),v2.end()
#define allrs(s) s.rbegin(),s.rend()
#define allrv(v) v.rbegin(),v.rend()
#define allrv2(v2) v2.rbegin(),v2.rend()
#define f1(i,t) for(int i=0;i<t;i++)
#define f2(i,n) for(int i=0;i<n;i++)
#define f3(j,t) for(int j=0;j<t;j++)
#define f4(j,t) for(int j=0;j<n;j++)
#define fs1(i,s) for(int i=0;i<s.size();i++)
#define fs2(i,s2) for(int i=0;i<s2.size();i++)
typedef long long ll;
int main()
{
  fast();
  ll t,n;
  cin>>t;
  while(t--)
  {
    cin>>n;
    vec v(n);
    f2(i,n)
    {
      cin>>v[i];
    }
    map<ll,ll>mp;
    vector<pair<ll,ll>>v2;
    f2(i,n)
    {
      mp[v[i]]++;
      auto g=make_pair(mp[v[i]],v[i]);
      v2.pb(g);
    }
   sort(allrv2(v2));
   sort(allv(v));
   auto h=unique(allv(v));
   v.erase(h,v.end());
   ll kk=v.size();
   cout<<max(min(kk-1,v2[0].first),min(kk,v2[0].first-1))<<endl;
  }
}


Comments

Submit
0 Comments
More Questions

237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes